﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FortuneHeaven.Admin.Common;
using FortuneHeavenCommon.DataMap;
using FortuneHeavenBAL;

namespace FortuneHeaven.Admin.Pages.Shop
{
    public partial class LotteryAudit : FortuneHeaven.Admin.Common.PageBase
    {
        public override string PageID
        {
            get { return "F25"; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            lblDeptName.Text = "抽奖发放";
            this.usPager.PagerChanged += new Admin.Controls.Pagination.PagerChangeHandler(usPager_PagerChanged);
            if (!this.IsPostBack)
            {
                this.drpPagerSize.Items.Clear();
                this.drpPagerSize.Items.Add(new ListItem("每页10行", "10"));
                this.drpPagerSize.Items.Add(new ListItem("每页20行", "20"));
                this.drpPagerSize.Items.Add(new ListItem("每页50行", "50"));

                if (!string.IsNullOrEmpty(Request["orderid"]))
                {
                    this.txtOrderID.Text = Request["orderid"];
                }


                if (!string.IsNullOrEmpty(Request["pagesize"]))
                {
                    this.drpPagerSize.SelectedIndex = int.Parse(Request["pagesize"]);
                }
                else
                {
                    this.drpPagerSize.SelectedValue = "10";
                }

                if (!string.IsNullOrEmpty(Request["current"]))
                {
                    ResetList(int.Parse(Request["current"]));
                }
                else
                {
                    ResetList();
                }
                //this.lvNews.ItemCommand += new EventHandler<ListViewCommandEventArgs>(lvNews_ItemCommand);

            }
        }
        private void usPager_PagerChanged(object sender, int currentpage)
        {
            Refresh();
        }

        public void Refresh()
        {
            LotteryOrderBO giftorder = new LotteryOrderBO();
            lvNews.DataSource = giftorder.GetLotteryOrderList(this.usPager.CurrentPageIndex, int.Parse(this.drpPagerSize.SelectedValue), this.txtOrderID.Text);
            lvNews.DataBind();
        }

        protected void drpPagerSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            ResetList();
        }

        public void ResetList()
        {
            ResetList(1);
        }

        public void ResetList(int current)
        {
            LotteryOrderBO giftorder = new LotteryOrderBO();
            this.usPager.RecordCount = giftorder.GetTotalSize(this.txtOrderID.Text);
            this.usPager.PageSize = int.Parse(this.drpPagerSize.SelectedValue);
            this.usPager.Initial();
            this.usPager.CurrentPageIndex = 1;
        }

        public void GiveNewScore(int userid,int gold)
        {
            if (userid <= 0)
            {
                return;
            }
            try
            {
                GameScoreInfoBO gamebo = new GameScoreInfoBO();
                GameScoreInfo score = gamebo.GetGameScore(userid);

                AccountsInfoBO acountbo = new AccountsInfoBO();
                AccountsInfo acount = acountbo.GetAcount(userid);
                if (score == null)
                {
                    score = new GameScoreInfo();
                    score.Score = 0;
                    score.Revenue = 0;
                    score.InsureScore = gold;
                    score.WinCount = 0;
                    score.LostCount = 0;
                    score.DrawCount = 0;
                    score.FleeCount = 0;
                    score.UserRight = 0;
                    score.AllLogonTimes = 0;
                    score.PlayTimeCount = 0;
                    score.UserID = userid;
                    score.OnLineTimeCount = 0;
                    if (acount != null)
                    {
                        score.RegisterIP = acount.RegisterIP;
                        score.RegisterDate = acount.RegisterDate;
                        score.LastLogonDate = acount.LastLogonDate;
                        score.LastLogonIP = acount.LastLogonIP;
                    }
                    else
                    {
                        score.RegisterIP = "1";
                        score.RegisterDate = DateTime.Now;
                        score.LastLogonDate = DateTime.Now;
                        score.LastLogonIP = "1";
                    }
                    gamebo.AddGameScore(score);
                }
                else
                {
                    score.InsureScore += gold;
                    gamebo.SaveGameScore(score);
                }
            }
            catch
            { 
            
            }
        }

        public void AuditItem(object sender, EventArgs arg)
        {
            LotteryOrderBO giftorder = new LotteryOrderBO();
            string ids = "";

            int userid = 0;
            int gold =0;
            int.TryParse(this.txtGold.Text.Trim(),out gold);
            foreach (ListViewItem item in this.lvNews.Items)
            {
                Control control = item.FindControl("chkSelect");

                if (control != null && control is CheckBox)
                {
                    if ((control as CheckBox).Checked)
                    {
                        string idstr = (item.FindControl("hfUserID") as HiddenField).Value;
                        int.TryParse(idstr, out userid);
                        GiveNewScore(userid, gold);
                        ids = ids+ (item.FindControl("hfIDs") as HiddenField).Value + ",";
                    }

                }
                //需要修改

            }
            ids = ids.TrimEnd(',');
            if (!string.IsNullOrEmpty(ids))
            {
                //Need add the audit user
                giftorder.AuditItems(ids, AuthorityCenter.CurrentUserID, AuthorityCenter.CurrentUserName, DateTime.Now);
                //OpLog.WriteLog(string.Format("修改礼物ID:{0}", ids));
                Refresh();
                ShowCurError("发放成功");
            }
        }

        public void DeleteItem(object sender, EventArgs arg)
        {
            LotteryOrderBO giftorder = new LotteryOrderBO();
            List<int> ids = new List<int>();
            foreach (ListViewItem item in this.lvNews.Items)
            {
                Control control = item.FindControl("chkSelect");

                if (control != null && control is CheckBox)
                {
                    if ((control as CheckBox).Checked)
                    {
                        ids.Add(int.Parse((item.FindControl("hfIDs") as HiddenField).Value));
                    }

                }
            }

            if (ids.Count > 0)
            {
                giftorder.Delete(ids);
                ResetList();
            }
        }

        public void SearchValue(object sender, EventArgs e)
        {
            ResetList();
        }
    }
}